package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Log;
import com.aftersale.demo.service.LogService;
import com.aftersale.demo.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 *
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log>
    implements LogService {

    @Autowired
    LogMapper logMapper;

    @Override
    public Result getLog(Integer pageNum, Integer pageSize) {
        Page<Log> logPage=page(new Page<>(pageNum,pageSize));
        return Result.success(logPage);
    }

    @Override
    public Result addLog(Log log) {
        String time= LocalDateTime.now().toString()
                .substring(0,19).replace("T"," ");
        log.setTime(time);
        save(log);
        return Result.success("添加成功");
    }

    @Override
    public Result deleteLog(int id) {
        logMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result updateLog(Log log) {
        Log dbLog=getById(log.getId());
        if(Objects.isNull(dbLog)){
            return Result.error("401","数据不存在");
        }else {
            updateById(log);
        }
        return Result.success("修改成功");
    }

    @Override
    public Result selectLog(String operation, Integer pageNum, Integer pageSize) {
        QueryWrapper<Log> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("operation",operation);
        Page<Log> logPage=page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(logPage);
    }
}




